﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using System.Configuration;
using Cosmetic.DataEntity;
using System.Collections;
using Cosmetic;

namespace WebApp.Product
{
    public partial class Productinfo : PageBase
    {
        public float TotalTags { get; set; }
        public float TotalComments { get; set; }

        private ProductManager m_ProductManager = new ProductManager();
        private CommentManager m_CommentManager = new CommentManager();
        private TagManager m_TagManager = new TagManager();
        private UserManager m_UserManager = new UserManager();
        private CategoryManager m_CategoryManager = new CategoryManager();
        private SaleManager m_salemanager = new SaleManager();

        string NotifyAddress = ConfigurationManager.AppSettings["NotifyAddress"];
        string ReturnAddress = ConfigurationManager.AppSettings["ReturnAddress"];

        Cosmetic.DataEntity.Product m_Product;
        Cosmetic.DataEntity.ProductInfo m_ProductInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["PID"]))
            {
                string a = this.Request.QueryString["PID"];
                Session["pid"] = this.Request.QueryString["PID"];
                this.m_Product = this.m_ProductManager.GetProductById1(new Guid(a));
                this.m_ProductInfo = this.m_ProductManager.GetProductInfoById(new Guid(a));
            }
            else
            {
                string b = Session["pid"].ToString();
                this.m_Product = this.m_ProductManager.GetProductById1(new Guid(b));
                this.m_ProductInfo = this.m_ProductManager.GetProductInfoById(new Guid(b));
            }

            this.HLThirdCategory.Text = m_Product.ProductCategory.Name.Remove(0, 4);
            this.HLThirdCategory.NavigateUrl = "~/Product/productclass.aspx?CID=" + m_Product.ProductCategory.CategoryId;

            //this.Literal2.Text = m_Product.Name;
            this.imgPicture.ImageUrl = m_ProductInfo.RecommendPicture1;
            this.LtName.Text = m_Product.Name;
            this.Literal2.Text = m_ProductInfo.ProductCode;
            this.Literal1.Text = m_ProductInfo.Name;
            if (m_ProductInfo.eprice == Convert.ToDecimal(0.01))
            {
                ImageButton1.Visible = false;
            }
            else
            {
                Literal4ask.Attributes.Add("style", "display:none");
                this.Literal4.Text = "￥" + Convert.ToString(m_ProductInfo.eprice);
            }
            this.Literal3.Text = Convert.ToString(m_ProductInfo.网络零售价);
            this.LiteralCommon2.Text = m_ProductInfo.Description;
            //this.LabelCountAll.Text = Convert.ToString(m_ProductInfo.网络零售价);
            // this.Literal6.Text = m_ProductInfo.Name;

            BindComment(m_Product);
        }

        /// <summary>
        /// 问题咨询。
        /// </summary>
        /// <param name="aProduct"></param>
        private void BindComment(Cosmetic.DataEntity.Product aProduct)
        {
            List<Comment> listComment = this.m_CommentManager.GetAllCommentByProduct(aProduct);//GetCommentByProduct(aProduct, 3);
            this.labAllComment.Text = listComment.Count.ToString();//显示总的评论数。
            this.Literal5.Text = listComment.Count.ToString();
            txtCommonNum.Text = listComment.Count.ToString();
            if (listComment.Count == 0)
            {
                //  LabelCommentbyUsers.Text = "暂时还没有用户评论。";
            }
            else
            {
                //  LabelCommentbyUsers.Text = "目前已有" + listComment.Count.ToString() + "名用户对该商品进行了评论。";
            }
            if ((listComment.Count % 3) == 0)
            {
                // LabelCountPager.Text = Convert.ToInt32((listComment.Count / 3)).ToString();
            }
            else
            {
                // LabelCountPager.Text = Convert.ToInt32((listComment.Count / 3) + 1).ToString();
            }
            this.lvCommend.DataSource = listComment; //显示评论内容。
            this.lvCommend.DataBind();
        }

        //加入购物车
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Buy();
            if (Session["buycarts"] != null)
            {

                Page.RegisterStartupScript("openconfirm ", " <script> if(confirm( '尊敬的用户，由于您选择的产品属于不同\\n的支付系统，所以您的购物车内原有的商\\n品会自动取消,是否继续操作？ ')){self.location='../Shopping/shopCart.htm'} else{self.location='../Shopping/shopCarts.htm'} </script> ");

                return;
            }
            else
            {
                Response.Redirect("~/Shopping/shopCart.htm");
            }
        }

        private void Buy()
        {
            try
            {
                int theCount = Convert.ToInt32(this.txtQuantity.Text.Trim());

                //add to buy cart
                if (null == Session["buycart"])
                {
                    Session["buycart"] = new Hashtable();
                }
                //加入尺寸
                if (null == Session["size"])
                {
                    Session["size"] = new Hashtable();
                }
                try
                {
                    if (((Hashtable)Session["buycart"]).Contains(m_Product.ProductId.ToString()))
                    {
                        int tint = int.Parse(((Hashtable)Session["buycart"])[m_Product.ProductId.ToString()].ToString());
                        ((Hashtable)Session["buycart"])[m_Product.ProductId.ToString()] = (++tint).ToString();

                    }
                    else
                    {
                        ((Hashtable)Session["buycart"]).Add(m_Product.ProductId.ToString(), theCount.ToString());

                    }
                    //Response.Redirect("~/Shopping/shopCart.htm");
                }
                catch (Exception)
                {
                    WebGeneric.WebMessageBox("购物篮添加失败!", this.Page);
                }

            }
            catch
            {
                WebGeneric.WebMessageBox("请正确填写购买数量!", this.Page);
            }

        }
    }//end class
}